package com.steel.yfmall.cloud.assistant.goods.impl;

import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductLibCategoryBean;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductLibProductFromBean;
import com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductLibProductFromBean;
import com.steel.yfmall.cloud.assistant.goods.ProductLibCategoryService;
import com.steel.yfmall.cloud.assistant.goods.ProductLibProductFromService;
import com.steel.yfmall.cloud.assistant.goods.mapper.ProductLibProductFromMapper;
import com.steel.yfmall.cloud.assistant.po.goods.ProductLibCategory;
import com.steel.yfmall.cloud.assistant.po.goods.ProductLibProductFrom;
import com.steel.yfmall.cloud.assistant.po.goods.ProductLibProductFromExample;
import com.steel.yfmall.cloud.components.utils.utils.MobileBaseBeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: ProductLibProductFromServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.biz.service.impl.goods
 * @Description: (用一句话描述该文件做什么)
 * @date 2020/12/3 10:53
 */
@Service
public class ProductLibProductFromServiceImpl implements ProductLibProductFromService {

    @Resource
    ProductLibProductFromMapper productLibProductFromMapper;

    @Resource
    private ProductLibCategoryService productLibCategoryService;


    @Override
    public List<ProductLibProductFrom> getList(ProductLibProductFrom productLibProductFrom) {
        ProductLibProductFromExample example = new ProductLibProductFromExample();
        example.createCriteria();
        return productLibProductFromMapper.selectByExample(example);
    }

    public List<ProductLibProductFrom> getListByPara(ParaProductLibProductFromBean paraBean) {
        ProductLibProductFromExample example = new ProductLibProductFromExample();
        getSearchData(example.createCriteria(),paraBean);
        return productLibProductFromMapper.selectByExample(example);
    }

    private void getSearchData(ProductLibProductFromExample.Criteria criteria, ParaProductLibProductFromBean paraBean) {
        if (criteria == null || paraBean == null){
            return;
        }
        if (CollectionUtils.isNotEmpty(paraBean.getCategoryIdList())){
            criteria.andCategoryIdIn(paraBean.getCategoryIdList());
        }

        if (CollectionUtils.isNotEmpty(paraBean.getProductFormCodeList())){
            criteria.andProductFormCodeIn(paraBean.getProductFormCodeList());
        }
    }

    /**
     * 获取产品形态对应的产品名称
     * @Title: getProductLibFromBeanMap
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.Map<java.lang.String,com.steel.yfmall.cloud.assistant.dto.result.goods.ResultProductLibProductFromBean>    返回类型
     * @author yuchaohua
     * @date 2021/2/19 18:41
     */
    @Override
    public Map<String, ResultProductLibProductFromBean> getProductLibFromBeanMap(ParaProductLibProductFromBean paraBean){
        Map<String, ResultProductLibProductFromBean> returnMapBean = null;
        List<ProductLibProductFrom> productLibProductFroms = getListByPara(paraBean);
        if (CollectionUtils.isNotEmpty(productLibProductFroms)){
            // 分类信息
            Map<Integer, ProductLibCategory> categoryBeanMap = productLibCategoryService.getProductLibCategoryBeanMap(new ParaProductLibCategoryBean());
            returnMapBean = new HashMap<>();
            for(ProductLibProductFrom item : productLibProductFroms){
                ResultProductLibProductFromBean returnBean = new ResultProductLibProductFromBean();
                MobileBaseBeanUtils.copyAllFields(item,returnBean);
                if (categoryBeanMap != null && returnBean.getCategoryId() != null && categoryBeanMap.containsKey(returnBean.getCategoryId())){
                    returnBean.setCategoryName(categoryBeanMap.get(returnBean.getCategoryId()).getCateName());
                }
                returnMapBean.put(item.getProductFormCode(),returnBean);
            }
        }
        return returnMapBean;
    }


}
